﻿#region License Information
// Copyright (c) 2005 - 2009, Allan Spartacus Mangune (allan@owlpal.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list 
//   of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of 
//   conditions and the following disclaimer in the documentation and/or other materials provided 
//   with the distribution.
//
// * Neither the name of Owlpal nor the names of its contributors may be used to endorse or 
//   promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
// FITNESS FOR AllowHtmlInComment PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Owlpal.Filters;
using Owlpal.Models;

namespace Owlpal.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class DashboardController : Controller
    {
        #region Blogs
        [Authentication]
        public ActionResult Index()
        {
            Session["BlogID"] = null;
            DashboardViewData viewData = new DashboardViewData();
            int count = 1;
            viewData.Blogs = Blog.FindAllByUserID(new Guid(Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey.ToString()),
                1, 100, out count);
            return View("Index", viewData);
        }

        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult CreateBlog()
        {
            return View(new Models.Blog());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddBlog(FormCollection collection)
        {
            Blog blog = new Blog();
            blog.Name = collection["Blog.Name"];
            blog.Title = collection["Blog.Title"];
            blog.Description = collection["Blog.Description"];
            blog.AllowAnonymousComment = false;
            blog.AllowHtmlInComment = false;
            blog.CopyrightOwner = HttpContext.User.Identity.Name;
            blog.CopyrightPeriod = DateTime.Now.ToShortDateString();
            blog.DateLocale = " "; //TODO: refactor
            blog.EmailAddressForEntryCreation = " "; //TODO: refactor
            blog.EmailAddressForCommentNotification = " "; //TODO: refactor
            blog.EnableComment = false;
            blog.EnableEmailReferral = false;
            blog.EnableFeeder = false;
            blog.EnableNewCommentNotification = false;
            blog.EnablePing = false;
            blog.EnablePublishing = false;
            blog.EnableRichEditor = false;
            blog.EnableTrackback = false;
            blog.EnbleAnonymousCommentModeration = false;
            blog.EntryDisplayCount = 10;
            blog.EntryDisplaySortOrder = " "; //TODO: refactor
            blog.EntryDisplayType = " "; //TODO: refactor
            blog.Theme = " ";//TODO: refactor
            blog.Timezone = " "; //TODO: refactor
            blog.IsPrimary = false;
            blog.Authors = new MembershipUserCollection();
            blog.Authors.Add(Membership.GetUser(HttpContext.User.Identity.Name));
            if (blog.Save() == AddBlogStatus.Added)
            {
                return RedirectToAction("Index", "Dashboard");
            }
            else
            {
                return View("CreateBlog");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="blogID"></param>
        /// <returns></returns>
        [Authentication]
        public ActionResult DeleteBlog(Guid blogID)
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="blogID"></param>
        /// <returns></returns>
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditBlog(Guid id)
        {
            Session["BlogID"] = id;
            Blog blog = Blog.Find(id);
            return View(blog);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theBlog"></param>
        /// <returns></returns>
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditBlog(FormCollection collection)
        {
            Blog blog = new Blog();
            blog.ID = new Guid(Convert.ToString(collection["blog_id"]));
            blog.Title = collection["title"];
            blog.Description = collection["description"];
            blog.AllowAnonymousComment = string.IsNullOrEmpty(Request.Form["allow_anonymous_comment"]) ? false : true;
            blog.IsPrivate = string.IsNullOrEmpty(Request.Form["blog_is_private"]) ? false : true;
            blog.AllowHtmlInComment = string.IsNullOrEmpty(Request.Form["allow_html_in_comment"]) ? false : true;
            blog.CopyrightOwner = Request.Form["copyright_owner"];
            blog.CopyrightPeriod = Request.Form["copyright_period"];
            blog.DateLocale = Request.Form["date_locale"];
            blog.EmailAddressForEntryCreation = Request.Form["email_address_for_entry_creation"];
            blog.EmailAddressForCommentNotification = Request.Form["email_address_for_comment_notification"];
            blog.EnableComment = string.IsNullOrEmpty(Request.Form["enable_comment"]) ? false : true ;
            blog.EnableEmailReferral = string.IsNullOrEmpty(Request.Form["enable_email_referral"]) ? false : true;
            blog.EnableFeeder = string.IsNullOrEmpty(Request.Form["enable_feeder"]) ? false : true;
            blog.EnableNewCommentNotification = string.IsNullOrEmpty(Request.Form["enable_new_comment_notification"]) ? false : true ;
            blog.EnablePing = string.IsNullOrEmpty(Request.Form["enable_ping"]) ? false : true ;
            blog.EnablePublishing = string.IsNullOrEmpty(Request.Form["enable_publishing"]) ? true : true;
            blog.EnableRichEditor = string.IsNullOrEmpty(Request.Form["enable_rich_editor"]) ? true : false;
            blog.EnableTrackback = string.IsNullOrEmpty(Request.Form["enable_trackback"]) ? true : false ;
            //if (Request.Form["Blog.EnbleAnonymousCommentModeration"] != null) blog.EnbleAnonymousCommentModeration = true;
            blog.EntryDisplayCount = Convert.ToInt16(Request.Form["entry_display_count"]);
            blog.EntryDisplaySortOrder = Request.Form["entry_display_sort_order"];
            blog.EntryDisplayType = Request.Form["entry_display_type"];
            blog.Theme = Request.Form["theme"];
            blog.Timezone = Request.Form["timezone"];
            blog.DefaultEntryStatus = Request.Form["default_entry_status"];
            blog.Edit();
            return EditBlog(blog.ID);
        }
        #endregion

        #region Entries
        /// <summary>
        /// 
        /// </summary>
        /// <param name="blogID"></param>
        /// <returns></returns>
        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult AddEntry(Guid blogID)
        {
            ViewData["BlogID"] = blogID;
            Entry entry = new Entry();
            return View(entry);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="blogID"></param>
        /// <returns></returns>
        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddEntry(FormCollection collection, Guid blogID)
        {
            Entry entry = new Entry();
            entry.Blog = new Blog();
            entry.Blog.ID = blogID;
            entry.Title = collection["Title"];
            entry.Text = collection["Text"];
            entry.Excerpt = collection["Excerpt"];
            entry.FlightEndDate = Convert.ToDateTime(collection["FlightEndDate"]);
            entry.FlightStartDate = Convert.ToDateTime(collection["FlightStartDate"]);
            entry.LinkName = collection["LinkName"];
            entry.IsCommentingAllowed = string.IsNullOrEmpty(collection["IsCommentingAllowed"]) ? false : true;
            entry.Status = collection["Status"];
            entry.IsCommentNotificationEnabled = string.IsNullOrEmpty(collection["IsCommentNotificationEnabled"]) ? false : true;
            entry.IsExcerptSyndicationEnabled = string.IsNullOrEmpty(collection["IsExcerptSyndicationEnabled"]) ? false : true;
            entry.IsPingingAllowed = string.IsNullOrEmpty(collection["IsPingingAllowed"]) ? false : true;
            entry.IsTrackbackAllowed = string.IsNullOrEmpty(collection["IsTrackbackAllowed"]) ? false : true;
            entry.CommentStatus = string.Empty;
            entry.CommentCount = 0;
            entry.Save();
            return View(entry);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="blogID"></param>
        /// <returns></returns>
        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EditEntry(Guid blogID)
        {
            
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Authentication]
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditEntry()
        {
            return View();
        }

        #endregion
    }


    public class DashboardViewData
    {
        public List<Blog> Blogs { get; set; }        
    }
}
